package com.sbhotel.chotellauncher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.constant.Constants;
import com.sbhotel.chotellauncher.entity.HomeMenuBean;
import com.sbhotel.chotellauncher.entity.LauncherMainMenuBean;

import java.util.List;

/**
 * @author leihuiming
 * @Description
 */
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {

    private Context context;
    //默认数据
    List<HomeMenuBean> list;
    //网络获取数据
    List<LauncherMainMenuBean.DataBean.ContListBean> dataList;

    public static final int TYPE_HEADER = 0; //说明是带有Header的
    public static final int TYPE_FOOTER = 1; //说明是带有Footer的
    public static final int TYPE_NORMAL = 2; //说明是不带有header和footer的

    private View mHeaderView;
    private View mFooterView;

    public MyRecyclerAdapter(Context context, List<LauncherMainMenuBean.DataBean.ContListBean> dataList) {
        this.context = context;
        list = Constants.initHomeMenuData();
        this.dataList = dataList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) {
            return new ViewHolder(mHeaderView);
        }
        if (mFooterView != null && viewType == TYPE_FOOTER) {
            return new ViewHolder(mFooterView);
        }
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_recycler, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_NORMAL) {
            if (holder instanceof ViewHolder) {
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                if (dataList != null && dataList.size() > 0) {
                    //holder.itemImg.setImageResource(dataList.get(position - 1).getCont_background_img());
                    LauncherMainMenuBean.DataBean.ContListBean bean = dataList.get(position - 1);

                    Glide.with(context).load(dataList.get(position - 1).getCont_background_img()).apply(RequestOptions.placeholderOf(list.get(position - 1).getImgId())).into(holder.itemImg);
                    holder.itemRL.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mOnItemClickLitener != null) {
                                mOnItemClickLitener.onItemClick(holder.itemRL, dataList.get(position - 1), position - 1);
                            }
                        }
                    });
                } else {
                    holder.itemImg.setImageResource(list.get(position - 1).getImgId());
                    holder.itemRL.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (mOnItemClickLitener != null) {
                                mOnItemClickLitener.onItemClick(holder.itemRL, null, position - 1);
                            }
                        }
                    });
                }
            }
            return;
        } else if (getItemViewType(position) == TYPE_HEADER) {
            return;
        } else {
            return;
        }

    }

    @Override
    public int getItemCount() {
        if (mHeaderView == null && mFooterView == null) {
            return list.size();
        } else if (mHeaderView == null && mFooterView != null) {
            return list.size() + 1;
        } else if (mHeaderView != null && mFooterView == null) {
            return list.size() + 1;
        } else {
            return list.size() + 2;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView itemImg;
        public RelativeLayout itemRL;

        public ViewHolder(View itemView) {
            super(itemView);

            //如果是headerview或者是footerview,直接返回
            if (itemView == mHeaderView) {
                return;
            }
            if (itemView == mFooterView) {
                return;
            }
            itemImg = itemView.findViewById(R.id.item_img);
            itemRL = (RelativeLayout) itemView.findViewById(R.id.itemRL);

        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, Object data, int position);

        //void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void setDataList(List<LauncherMainMenuBean.DataBean.ContListBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    //HeaderView和FooterView的get和set函数
    public View getHeaderView() {
        return mHeaderView;
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getFooterView() {
        return mFooterView;
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null) {
            return TYPE_NORMAL;
        }
        if (position == 0) {
            //第一个item应该加载Header
            return TYPE_HEADER;
        }
        if (position == getItemCount() - 1) {
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }
}
